package com.jun.rabbitmq.producer.config;

import com.jun.common.rabbitmq.constant.ExchangeConstant;
import com.jun.common.rabbitmq.constant.QueueConstant;
import com.jun.common.rabbitmq.constant.RouteConstant;
import com.jun.common.rabbitmq.enums.ArgumentEnum;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yuejun.li
 * @date 2024/4/24 15:17:25
 * 死信配置
 */
@Configuration
public class DeadRabbitConfig {

    //===========================死信队列===============================

    //死信队列
    @Bean
    public Queue deadQueue(){
        return new Queue(QueueConstant.DEAD_QUEUE,true);
    }

    //死信交换机
    @Bean
    public DirectExchange deadExchange(){
        return new DirectExchange(ExchangeConstant.DEAD_EXCHANGE,true,false);
    }

    //绑定死信队列到死信交换机
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with(RouteConstant.DEAD_KEY);
    }


    //=======================死信队列-延时队列============================

    @Bean
    public Queue ttlQueue(){
        return new Queue(QueueConstant.TTL_QUEUE, true);
    }

    /**
     * 创建交换机
     */
    @Bean
    public DirectExchange ttlExchange() {
        return new DirectExchange(ExchangeConstant.TTL_EXCHANGE, true, false);
    }

    /**
     * 队列绑定交换机
     */
    @Bean
    public Binding ttlBinding() {
        return BindingBuilder.bind(ttlQueue()).to(ttlExchange()).with(RouteConstant.TTL_KEY);
    }


    //=======================死信队列-延迟插件队列========================

    @Bean
    public Queue delayQueue(){
        return new Queue(QueueConstant.DELAY_QUEUE,true);
    }

    @Bean
    public CustomExchange delayedExchange(){
        Map<String, Object> args = new HashMap<>();
        // 自定义交换机的类型
        args.put(ArgumentEnum.X_DELAYED_TYPE_CONSTANT, "direct");
        return new CustomExchange(ExchangeConstant.DELAY_EXCHANGE, ArgumentEnum.X_DELAYED_MESSAGE_CONSTANT, true, false, args);
    }

    @Bean
    public Binding delayedBinding(){
        return BindingBuilder.bind(delayQueue()).to(delayedExchange()).with(RouteConstant.DELAY_KEY).noargs();
    }

    //=======================死信队列-限流队列============================

    @Bean
    public Queue maxQueue(){
        return new Queue(QueueConstant.MAX_QUEUE, true);
    }

    /**
     * 创建交换机
     */
    @Bean
    public DirectExchange maxExchange() {
        return new DirectExchange(ExchangeConstant.MAX_EXCHANGE, true, false);
    }

    /**
     * 队列绑定交换机
     */
    @Bean
    public Binding maxBinding() {
        return BindingBuilder.bind(maxQueue()).to(maxExchange()).with(RouteConstant.MAX_KEY);
    }

}
